//
//  ZJSecuritycodeViewController.m
//  ZiJinLian
//
//  Created by lzy on 2017/3/14.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ZJSecuritycodeViewController.h"
#import "ZJsecuritycodeView.h"
#import "ZJRegisterSecondViewController.h"
#import "NewRegistViewController.h"
#import "SetPayWordViewController.h"
#import "AccountViewController.h"
@interface ZJSecuritycodeViewController ()<UITextFieldDelegate>
@property(assign, nonatomic) NSInteger timeCount;
@property(strong, nonatomic) NSTimer *timer;
@property(nonatomic,copy)NSString *code;

@end

@implementation ZJSecuritycodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    
    //按钮方法
    [self BtnTarget];
    // 代理
    [self setTFdeleget];
    
    //文本框输入状态监测
    [self.securitycodeView.securitycodeTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //清空输入框
    self.securitycodeView.securitycodeTF.text=@"";
    //btn.userInteractionEnabled = NO;
   
    
    
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:self.curphoneStr forKey:@"phone"];
    
    [self doHttpRequestWithApi:API_SEND_SMS method:POST dic:parametersDic successBlock:^(NSDictionary *dic) {
        self.timeCount = 60;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reduceTime:) userInfo:nil repeats:YES];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:MyString(@"验证码发送成功，请查收") delegate:self cancelButtonTitle:nil otherButtonTitles:MyString(@"确定"), nil];
        [alert show];
        
        
    }];
}
-(void)createView{
    // Do any additional setup after loading the view.
    [self initTitle:MyString(@"注册")];
    _securitycodeView=[[ZJsecuritycodeView alloc]init];
    _securitycodeView.frame=self.view.frame;
    [self.view addSubview:self.securitycodeView];
    //传数值，接受上个页面的 电话号码
    if (self.binkCart==1||self.binkCart==4) {
        [self.securitycodeView.nextBtn setTitle:MyString(@"确定") forState:0];
        [self initTitle:MyString(@"验证手机")];
    }else if (self.binkCart==2){
        [self.securitycodeView.nextBtn setTitle:MyString(@"下一步") forState:0];
        [self initTitle:MyString(@"验证手机")];
    }
    //NSString *str=[NSString stringWithFormat:@"%@",self.curphoneStr];
    NSString*replacedStr;
    replacedStr= [self.curphoneStr stringByReplacingOccurrencesOfString: [self.curphoneStr substringWithRange:NSMakeRange(3,4)]withString:@"****"];
    
    self.securitycodeView.phoneLab.text=[NSString stringWithFormat:@"请输入%@收到的验证码",replacedStr];

}
-(void)doOpeartion{
 
    [self doKeyBoardDown];
    
    
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    return YES;
}


-(void)BtnTarget
{
    [_securitycodeView.getsecuritycodeBtn addTarget:self action:@selector(SendBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_securitycodeView.nextBtn addTarget:self action:@selector(nextClick:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)setTFdeleget
{
    self.securitycodeView.securitycodeTF.delegate=self;
    
}

//输入框代理方法
- (void) textFieldDidChange:(id) sender {
    UITextField *_field = (UITextField *)sender;
    NSString *str=_field.text;
    if (str) {
        [self.securitycodeView.nextBtn setBackgroundColor:button_Red_Color];
        [self.securitycodeView.nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }
}
#pragma -------------------获取验证码 按钮 方法————————————————————————
-(void)SendBtnClick:(UIButton *)btn
{
    //清空输入框
    self.securitycodeView.securitycodeTF.text=@"";
    btn.userInteractionEnabled = NO;
    
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:self.curphoneStr forKey:@"phone"];
    
    [self doHttpRequestWithApi:API_SEND_SMS method:POST dic:parametersDic successBlock:^(NSDictionary *dic) {
        self.timeCount = 60;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reduceTime:) userInfo:btn repeats:YES];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:MyString(@"验证码发送成功，请查收") delegate:self cancelButtonTitle:nil otherButtonTitles:MyString(@"确定"), nil];
            [alert show];
            
    
    }];
    
}

- (void)reduceTime:(NSTimer *)codeTimer {
    self.timeCount--;
    if (self.timeCount == 0) {
        [_securitycodeView.getsecuritycodeBtn setTitle:MyString(@"重新获取") forState:UIControlStateNormal];
        [_securitycodeView.getsecuritycodeBtn setTitleColor:[UIColor colorWithRed:205/255.0f green:8/255.0f blue:20/255.0f alpha:1] forState:UIControlStateNormal];
        UIButton *info = codeTimer.userInfo;
        info.enabled = YES;
        _securitycodeView.getsecuritycodeBtn.userInteractionEnabled = YES;
        [self.timer invalidate];
    } else {
        _securitycodeView.getsecuritycodeBtn.titleLabel.font = [UIFont systemFontOfSize: 13.0];
        NSString *str = [NSString stringWithFormat:@"%@(%lu)", MyString(@"重新获取"),self.timeCount];
        [_securitycodeView.getsecuritycodeBtn setTitle:str forState:UIControlStateNormal];
        _securitycodeView.getsecuritycodeBtn.userInteractionEnabled = NO;
        
    }
}





-(void)nextClick:(UIButton *)btn
{
    if (self.securitycodeView.securitycodeTF.text.length==0) {
        [self showMessage:MyString(@"请输入验证码")];
        return;
    }
    
    
    
        NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
        [parametersDic setObject:self.curphoneStr forKey:@"phone"];
        [parametersDic setObject:self.securitycodeView.securitycodeTF.text forKey:@"smsCode"];

    
     [self doHttpRequestWithApi:API_CHECK_SMS method:GET dic:parametersDic successBlock:^(NSDictionary *dic) {
         if (self.binkCart==1) {
             [self bindBankRequest];
         }else if (self.binkCart==2){
             SetPayWordViewController*vc=[[SetPayWordViewController alloc]init];
             vc.statue=1;
             vc.cmsCode=self.securitycodeView.securitycodeTF.text;
             [self.navigationController pushViewController:vc animated:YES];
         }else if (self.binkCart==4){
             SetPayWordViewController*vc=[[SetPayWordViewController alloc]init];
             vc.statue=10;
             vc.cmsCode=self.securitycodeView.securitycodeTF.text;
             [self.navigationController pushViewController:vc animated:YES];
         }else{
             NewRegistViewController * senconRegisterVC=[[NewRegistViewController alloc]init];
             senconRegisterVC.inviteCode=self.inviteCode;
             senconRegisterVC.phone=self.curphoneStr;
             senconRegisterVC.smsCode=self.securitycodeView.securitycodeTF.text;
             [self.navigationController pushViewController:senconRegisterVC animated:YES];
         }
        
    }];
    

    
    
}

-(void)bindBankRequest{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    dic[@"creditName"]=[NSString stringWithFormat:@"%@",[self.dic objectForKey:@"creditName"]];
    dic[@"creditAccount"]=[NSString stringWithFormat:@"%@",[self.dic objectForKey:@"creditAccount"]];
    dic[@"creditBank"]=[NSString stringWithFormat:@"%@",[self.dic objectForKey:@"creditBank"]];
    dic[@"smsCode"]=self.securitycodeView.securitycodeTF.text;
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    NSLog(@"------%@",dic);
    [self doHttpRequestWithApi:BINDBANKCARD method:GET dic:dic successBlock:^(NSDictionary *dic) {
        [self showToast:@"绑定成功!"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            for (UIViewController *controller in self.navigationController.viewControllers) {
                if ([controller isKindOfClass:[AccountViewController class]]) {
                    AccountViewController *A =(AccountViewController *)controller;
                    [self.navigationController popToViewController:A animated:YES];
                }
            }
            
        });
    }];
}


@end
